<?php

/**
 * stkdept actions.
 *
 * @package    tinystockmis
 * @subpackage stkdept
 * @author     Linfeng Wu
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class stkdeptActions extends sfActions {
    public function executeIndex(sfWebRequest $request) {
        /*
         * $this->stk_depts = Doctrine::getTable('StkDept')
                ->createQuery('a')
                ->execute();
         *
         */
    }

    public function executeEditIndex(sfWebRequest $request)
    {
        
    }

    public function executeGetStkDeptList(sfWebRequest $request) {
        $this->pager = new sfDoctrinePager(
                "StkDept",
                $request->getParameter("rows",sfConfig::get("app_max_vouchers_per_page", 10))
        );
        
        $q = Doctrine::getTable("StkDept")->getStkDeptQuery();
        $this->pager->setQuery($q);
        $this->pager->setPage($request->getParameter("page",1));
        $this->pager->init();
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new StkDeptForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new StkDeptForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($stk_dept = Doctrine::getTable('StkDept')->find(array($request->getParameter('id'))), sprintf('Object stk_dept does not exist (%s).', $request->getParameter('id')));
        $this->form = new StkDeptForm($stk_dept);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($stk_dept = Doctrine::getTable('StkDept')->find(array($request->getParameter('id'))), sprintf('Object stk_dept does not exist (%s).', $request->getParameter('id')));
        $this->form = new StkDeptForm($stk_dept);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($stk_dept = Doctrine::getTable('StkDept')->find(array($request->getParameter('id'))), sprintf('Object stk_dept does not exist (%s).', $request->getParameter('id')));
        //todo 
        //检查该库存中没有相关的件号，否则不预删除

        
        $stk_dept->delete();

        $this->redirect('stkdept/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $stk_dept = $form->save();

            //$this->redirect('stkdept/edit?id='.$stk_dept->getId());
            $this->redirect('stkdept/index');
        }
    }
}
